﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering.View_Operaction;
using System.Data;
using System.Web.Services;

namespace Catering.Catering_Page.Kufang
{
    public partial class Zpsq : UserIsNull
    {
        Catering.View_Operaction_Common voc = new View_Operaction_Common();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["PersonnelID"] != null)
                {
                    string userid = Session["PersonnelID"].ToString();
                    int companyid = 0;
                    int groupid = 0;
                    int classid = 0;
                    bool isAdmin = false;
                    Bind_Data_to_Controls.GetCompanyByCurrentUser(userid, out companyid, out groupid, out classid, out isAdmin);
                    if (isAdmin)
                    {
                        voc.DropDownListDataBind_Group(dr_Group, companyid);
                        voc.DropDownListDataBind_Class(dr_Class, dr_Group.Items[0].Value);
                    }
                    else
                    {
                        Bind_Data_to_Controls.Bind_Area_ddl(dr_Group, isAdmin, groupid, companyid);
                        Bind_Data_to_Controls.Bind_Class_ddl(dr_Group.SelectedItem, dr_Class, isAdmin, classid);
                    }
                }

                this.but_Seach_Click(null, null);
            }
        }

        //添加杂品
        protected void but_add_Info_click(object sender, EventArgs e)
        {
            Catering_Rules.SundryGoods crsg = new Catering_Rules.SundryGoods();
            Catering_Model.SundryGoods md_cmsg = new Catering_Model.SundryGoods();

            md_cmsg.id = crsg.GetMaxId();
            md_cmsg.GroupID = Convert.ToInt32(this.dr_Group.SelectedValue);
            md_cmsg.ClassID = Convert.ToInt32(this.dr_Class.SelectedValue);
            md_cmsg.temp1 = DateTime.Now.ToShortDateString();
            md_cmsg.PID = Convert.ToInt32(Session["UserID"].ToString());
            md_cmsg.Specifications = txt_CMP.Value.Trim();
            md_cmsg.SName = txt_Name.Value.Trim();
            md_cmsg.temp2 = this.txt_type.Value.Trim();
            md_cmsg.Number = txt_Number.Value.Trim();
            md_cmsg.Description = txt_yongtu.Value.Trim();
            md_cmsg.State = 0;//0:待审批，1：审批
            md_cmsg.RAWID = 1;//没有作用
            crsg.Add(md_cmsg);
            View_Operaction_Common.Show(this, "添加成功");
            this.but_Seach_Click(null, null);
        }

        //查询
        protected void but_Seach_Click(object sender, EventArgs e)
        {
            DataTable dt_seacher = this.seach_reqest_Info(this.DDL_an.SelectedValue, this.txt_Seacher.Text.Trim());
            this.Repeater1.DataSource = dt_seacher;
            this.Repeater1.DataBind();
        }

        public DataTable seach_reqest_Info(string strSelect, string strSeachContent)
        {
            DataTable dt_seacher = new DataTable();
            List<string> list_string = new List<string>();
            list_string.Add("未审批");
            list_string.Add("已审批");
            list_string.Add("不审批");

            string str = " 1=1";

            if (Session["PersonnelID"] != null)
            {
                string userid = Session["PersonnelID"].ToString();
                int companyid = 0;
                int groupid = 0;
                int classid = 0;
                bool isAdmin = false;
                Bind_Data_to_Controls.GetCompanyByCurrentUser(userid, out companyid, out groupid, out classid, out isAdmin);
                if (!isAdmin) str = string.Format(" GroupID={0} AND ClassID={1} ", groupid, classid);
            }

            if (strSeachContent != "")
            {
                switch (strSelect)
                {
                    case "temp1":
                        str = str + " and temp1= '" + Convert.ToDateTime(strSeachContent).ToShortDateString() + "'";
                        break;
                    case "State":
                        str = str + " and State= " + list_string.IndexOf(strSeachContent);
                        break;
                    case "SName":
                        str = str + " and SName Like '%" + strSeachContent + "%'";
                        break;
                    case "SType":
                        str = str + " and temp2 like '%" + strSeachContent + "%'";
                        break;
                    case "PID":
                        DataTable dt_Personnel = new Catering_Rules.T_Personnel().GetList("Name like '%" + strSeachContent + "%'").Tables[0];
                        if (dt_Personnel != null && dt_Personnel.Rows.Count > 0)
                        {
                            string stridList = "";
                            foreach (DataRow row in dt_Personnel.Rows)
                            {
                                stridList += row["id"].ToString() + ",";
                            }
                            str = str + " and PID in (" + stridList.TrimEnd(',') + ")";
                        }
                        else
                        {
                            str += " and 1=2";
                        }
                        break;
                }
            }
            DataSet ds = new Catering_Rules.SundryGoods().GetList(str);
            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    dt_seacher = ds.Tables[0];
                }
            }
            return dt_seacher;
        }


        //更换查询条件
        protected void DDL_an_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txt_Seacher.Text = "";
            if (DDL_an.SelectedIndex == 2)
            {
                this.txt_Seacher.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-MM-dd'})");
            }
            else
            {
                this.txt_Seacher.Attributes.Clear();
            }
        }

        //审批
        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "shenpi")
            {
                string cid = e.CommandArgument.ToString();
                new View_Operaction.DataBase_Manage_Operaction().Update_request_Stat(cid);
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('审批成功！');</script>");
                this.but_Seach_Click(null, null);
            }
            else if (e.CommandName == "del")
            {
                string cid = e.CommandArgument.ToString();
                int id = 0;
                Int32.TryParse(cid, out id);
                if (id > 0)
                {
                    new Catering_Rules.SundryGoods().Delete(id);
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除成功！');</script>");
                    this.but_Seach_Click(null, null);
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除失败！');</script>");
                }
                
            }
        }


        //获取班组名称
        public string GetClassName(string ClassID)
        {
            return new Catering_Rules.ClassInfo().GetModel(Convert.ToInt32(ClassID)).CName;
        }

        //获取申请人名称
        public string GetPersonnelNaem(string PersonnelID)
        {
            Catering_Model.E_Personnel md = new Catering_Rules.T_Personnel().GetModel(Convert.ToInt32(PersonnelID));
            return md == null ? "&nbsp;" : md.Name;
        }

        protected void dr_Group_SelectedIndexChanged(object sender, EventArgs e)
        {
            voc.DropDownListDataBind_Class(dr_Class, this.dr_Group.SelectedValue);
        }

        public bool IsAdmin()
        {
            if (Session["RoleID"] != null && !string.IsNullOrEmpty(Session["RoleID"].ToString()))
            {
                int role = 0;
                Int32.TryParse(Session["RoleID"].ToString(), out role);
                Catering_Model.Role model = new Catering_Rules.Role().GetModel(role);
                if (model!=null && model.temp1>5) return true;
            }
            return false;
        }

        //public bool GetZPVerification(string state)
        //{
        //    if (Catering.Toolss.IsVerification(Session["RoleID"].ToString(), "Zpsq.aspx", "审批") && state == "0") return true;
        //    return false;
        //}
    }
}